package io.github.definitlyevil.ziminmod.cif;

import de.tr7zw.nbtapi.NBTCompound;
import io.github.definitlyevil.customitemframework.BaseBlock;
import io.github.definitlyevil.customitemframework.CIFItemInstance;
import io.github.definitlyevil.customitemframework.CustomItemFramework;
import io.github.definitlyevil.ziminmod.cif.tools.ZiMinPickaxe;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.ItemStack;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class ZiMinOre extends BaseBlock {
    @Override
    public String getDisplayName(NBTCompound nbtCompound) {
        return "\u00a7e\u00a7l籽岷矿";
    }

    @Override
    public void onBroken(CIFItemInstance instance, Block block, Entity by) {
        if(by == null) { // 如果是被炸毁或者是非人为破坏，那么就不掉落任何东西。
            return;
        }
        if(!HumanEntity.class.isAssignableFrom(by.getClass())) {
            return;
        }
        HumanEntity human = (HumanEntity) by;
        ItemStack item = human.getInventory().getItemInMainHand();
        // 检查 CIF 物品
        CIFItemInstance cif = CustomItemFramework.getCIFDataFromItem(item);
        if(cif == null) { // it already checked whether item is null or not
            // 原版物品
            Material type = item.getType();
            // 只能用钻石镐挖
            if(type == Material.DIAMOND_PICKAXE) {
                Location location = block.getLocation();
                location.getWorld().dropItem(location, generateItemStack(null, false));
            }
        } else {
            // CIF 物品，检查是不是籽岷镐
            if(ZiMinPickaxe.class.isAssignableFrom(cif.type.getClass())) {
                Location location = block.getLocation();
                ItemStack itemDouble = generateItemStack(null, false);
                location.getWorld().dropItem(location, itemDouble);
            }
        }
    }

    private static final List<String> LORE = Arrays.asList(
        "\u00a7e籽岷矿内含有大量未经提炼",
        "\u00a7e的“实例”。通过高级熔炉，",
        "\u00a7e可以熔炼出“籽岷锭”，用来",
        "\u00a7e制作“针男人”工具和“针男",
        "\u00a7e人”套装。"
    );
    @Override
    public List<String> getLore(CIFItemInstance instance) {
        return LORE;
    }

    @Override
    public Material getModelMaterial() {
        return Material.DIAMOND_ORE;
    }

    @Override
    public int getDefaultModelData() {
        return 1;
    }

    @Override
    public int getCustomModelData(CIFItemInstance instance) {
        return new Random().nextInt(4) + 1;
    }
}
